package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.form.shopping.CancelForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.zookeeper.data.Id;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName: OrderController
 * @Description:
 * @author: by zjl
 * @Date: 2021/5/23   14:25
 */
@RestController
@RequestMapping("/shopping")
@Api(tags = "OrderController", description = "订单控制层")
public class OrderController {

    @Reference(timeout = 1200000, check = false)
    private OrderCoreService orderCoreService;

    @Reference(timeout = 1200000, check = false)
    private OrderQueryService orderQueryService;


    /**
     * 创建订单
     */
    @ApiOperation("创建订单！")
    @PostMapping("/order")
    public ResponseData order(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {
        String userInfo = String.valueOf(servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY));
        //String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);//userInfo
        Long uid = Long.parseLong(userInfo);
//        JSONObject object = JSON.parseObject(userInfo);
//        long uid = Long.parseLong(object.get("uid").toString());
        request.setUserId(uid);

        // 通过UUID.randomUUID().toString()设置uniqueKey
        request.setUniqueKey(UUID.randomUUID().toString());

        //orderId：返回参数只需要这个
        CreateOrderResponse response = orderCoreService.createOrder(request);//key

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getOrderId());
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 查询订单详情
     * @param orderId
     * @param request
     * @return
     */
    @GetMapping("/order/{id}")
    public ResponseData getOrderDetail(@PathVariable(value = "id") String orderId, HttpServletRequest request) {
        // 获取当前登陆用户信息 做一个校验
//        Object userInfo = request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(orderId);
        OrderDetailFinalResponse orderDetailFinalResponse = orderQueryService.queryOrderDetail(orderDetailRequest);
        if (!orderDetailFinalResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(orderDetailFinalResponse.getMsg());
        }

        // 封装
        return new ResponseUtil<>().setData(orderDetailFinalResponse.getOrderDetailFinalDTO());
    }


    /**
     * 取消订单
     */
    @ApiOperation("取消订单")
    @PostMapping("/cancelOrder")
    //@ApiImplicitParam(name = "id", value = "订单ID", paramType = "path")

    public ResponseData cancleOrder(@RequestBody CancelForm cancelForm) {
        CancelOrderRequest request = new CancelOrderRequest();
        request.setOrderId(cancelForm.getOrderId());

        CancelOrderResponse response = orderCoreService.cancelOrder(request);
        return new ResponseUtil<>().setData(response);
    }


    @GetMapping("/order")
    public ResponseData getAllOrder(int size,int page,HttpServletRequest servletRequest){
        Integer userInfo = (Integer) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        long uid = userInfo.intValue();
        OrderListRequest request = new OrderListRequest();
        request.setUserId(uid);
        request.setPage(page);
        request.setSize(size);
        OrderListResponse response=orderQueryService.query(request);
        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            OrderListVo orderListVo = new OrderListVo();
            orderListVo.setData(response.getDetailInfoList());
            orderListVo.setTotal(response.getTotal());
            return new ResponseUtil<>().setData(orderListVo);
        }
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);
        DeleteOrderResponse response = orderCoreService.deleteOrder(deleteOrderRequest);
        if (!response.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil<>().setData(response, "success");
    }
}




